package virtualTabletop;

/**
 * 
 * making positions
 * 
 */
public class Position
{
	private int x;
	private int y;

	/**
	 * Constructor for positions.
	 * 
	 * @param x
	 *            - x coord.
	 * @param y
	 *            - y coord.
	 */
	public Position(int x, int y)
	{
		this.x = x;
		this.y = y;
	}

	/**
	 * Gets the x-position.
	 * 
	 * @return x x-coord.
	 */
	public int getX()
	{
		return x;
	}

	/**
	 * Gets the y-position.
	 * 
	 * @return y y-coord.
	 */
	public int getY()
	{
		return y;
	}

	/**
	 * A way to move a position
	 * 
	 * @param direction
	 *            the direction to move the position towards.
	 */
	public void move(Direction direction)
	{
		x += Direction.dx(direction);
		y += Direction.dy(direction);
	}

	/**
	 * a way to move a position towards another position
	 * 
	 * @param p
	 *            the other position
	 */
	public void moveTowards(Position p)
	{
		if (p.getX() > x)
		{
			x++;
		}
		if (p.getY() > y)
		{
			y++;
		}
		if (p.getX() < x)
		{
			x--;
		}
		if (p.getY() < y)
		{
			y--;
		}
	}

	/**
	 * tests if a position is close to another position
	 * 
	 * @param pos
	 *            the other position
	 * @return true if it is close to.
	 */
	public boolean closeTo(Position pos)
	{
		return pos.getX() >= getX() - 1 && pos.getX() <= getX() + 1
				&& pos.getY() >= getY() - 1 && pos.getY() <= getY() + 1;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
		{
			return true;
		}
		if (obj == null)
		{
			return false;
		}
		if (getClass() != obj.getClass())
		{
			return false;
		}
		Position other = (Position) obj;
		if (x != other.x)
		{
			return false;
		}
		if (y != other.y)
		{
			return false;
		}
		return true;
	}
}